package chapter9;

import java.util.Random;
import java.util.Scanner;

public class BankDemo {
	public int withdrawal(int balance) {
		Scanner sc = new Scanner(System.in);
		System.out.println("--------- 取款 ---------");
		System.out.println(" 请输入取款金额：");
		int money = sc.nextInt();
		if (balance > money) {
			balance -= money;
			System.out.println(" 取款成功 ");
		} else {
			System.out.println(" 余额不足，取款失败 ");
		}
		return balance;
	}

	public int deposit(int balance) {
		Scanner sc = new Scanner(System.in);
		System.out.println("--------- 存款 ---------");
		System.out.println(" 请输入存款金额：");
		int money = sc.nextInt();
		balance += money;
		System.out.println(" 存款成功 ");
		return balance;
	}

	public void queryBalance(int balance) {
		System.out.println("--------- 查询 ---------");
		System.out.println(" 您的余额：" + balance);
	}

	public static void main(String[] args) {
		BankDemo bd = new BankDemo();
		Scanner sc = new Scanner(System.in);
		System.out.println("*** 欢迎使用本银行 ***");
		int choose;
		Random rdm = new Random();
		int balance = rdm.nextInt(1000);// 余额为 0~999 之间的数字
		System.out.println(" 服务种类：");
		System.out.println("【1】查看余额 ");
		System.out.println("【2】取款 ");
		System.out.println("【3】存款 ");
		System.out.println("【0】退出 ");
		do {
			System.out.println(" 请选择：");
			choose = sc.nextInt();
			switch (choose) {
			case 1:
				bd.queryBalance(balance);
				break;
			case 2:
				balance = bd.withdrawal(balance);
				break;
			case 3:
				balance = bd.deposit(balance);
				break;
			case 0:
				System.out.println(" 欢迎下次继续使用！ ");
				break;
			default:
				System.out.println(" 输入有误，请重新输入 ");
				break;
			}
		} while (choose != 0);
	}
}
